package chaoyue.study.service;

import chaoyue.study.grpc.HelloRequest;
import chaoyue.study.grpc.HelloResponse;
import chaoyue.study.grpc.HelloServiceGrpc;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;

/**
 * gRPC调用客户端
 */
@Service
public class HelloClientService {

    /**
     * 通过Stub进行方法调用
     * local-server为配置文件中client定义的。即会通过server的名称获取到对应的调用地址
     * HelloService即proto中定义的Service名称，后缀Grpc以及BlockingStub都为固定模版代码
     */
    @GrpcClient("hello-server")
    private HelloServiceGrpc.HelloServiceBlockingStub helloServiceBlockingStub;

    public String sayHello(String name) {
        HelloRequest request = HelloRequest.newBuilder().setName(name).build();
        // 通过stub即可发起调用，方法名即为proto中定义的方法名
        HelloResponse response = helloServiceBlockingStub.sayHello(request);
        return response.getMessage();
    }
}
